<script setup>
import { ref, onMounted, watch, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import {
  useAllUsersStore,
  useNameStore,
  useUserStore,
  usePrivateNewsStore
} from '@/stores'
import {
  userGetTwoPeopleNewsService,
  sendTwoPeopleNewsService
} from '@/api/news.js'
const id = ref()
const route = useRoute()
const userInfo = ref({})
const userStore = useUserStore()
const allUsersStore = useAllUsersStore()
const nameStore = useNameStore()
const privateNewsStore = usePrivateNewsStore()
const news = ref('')
const Main = ref()
const rol = ref()
watch(
  rol,
  () => {
    rolling()
  },
  { deep: true }
)
const scrollbarRef = ref(null)
const rolling = () => {
  nextTick(() => {
    // 获取滚动条DOM元素
    const scrollbar = scrollbarRef.value.$el
    // 获取滚动条内部的滚动容器
    const container = scrollbar.querySelector('.el-scrollbar__view')
    // 计算需要滚动的距离
    const scrollHeight = container.scrollHeight
    const height = container.clientHeight
    // 滚动到底部
    scrollbarRef.value.scrollTo(0, scrollHeight)
    console.log('scrollBottom:' + scrollHeight)
  })
}
/* const defaultArray = [
  {
    id: 16,
    content: '喂喂喂!!',
    fromUser: 'ma',
    fromImage: 'ma',
    toUser: 'lee',
    toImage: 'lee',
    messageType: null,
    sendTime: '2024-06-15T17:35:43',
    isRead: 1
  },
  {
    id: 17,
    content: '喂喂喂!!',
    fromUser: 'lee',
    fromImage: 'lee',
    toUser: 'ma',
    toImage: 'ma',
    messageType: null,
    sendTime: '2024-06-15T17:36:04',
    isRead: 1
  },
  {
    id: 18,
    content: '喂喂喂!!',
    fromUser: 'lee',
    fromImage: 'lee',
    toUser: 'ma',
    toImage: 'ma',
    messageType: null,
    sendTime: '2024-06-15T17:36:05',
    isRead: 1
  }
] */
onMounted(() => {
  id.value = route.query.id
  console.log('route.query')
  console.log(route.query)
  console.log(id.value)
})
watch(
  () => userStore.book,
  async (newVal, oldVal) => {
    // 当查询参数变化时，这个函数会被调用
    console.log('Query param changed from', oldVal, 'to', newVal)
    console.log('当前私聊对象的账号是：')
    console.log(userStore.book)
    userInfo.value = await allUsersStore.findUser(userStore.book)
    console.log('当前私聊用户的信息是：')
    console.log(userInfo.value)
    // 获取用户信息
    const res = await userGetTwoPeopleNewsService(
      userStore.userName,
      userStore.book
    )
    console.log('用户的聊天信息是：')
    console.log(res.data.data)
    privateNewsStore.initPrivateNewsList(res.data.data)
    console.log('privateNews.value：聊天信息是：')
    console.log(privateNewsStore.privateNewsList)
  },
  { immediate: true, deep: true } // immediate: true 表示不立即执行回调，deep: true 用于深度监听对象的变化
)

const sendNews = async () => {
  console.log('要发送的东西是:' + news.value)
  if (news.value.trim()) {
    const res = await sendTwoPeopleNewsService(news.value)
    //ws.send(news.value)
    console.log('send返回的报文是：' + res)
  }
  news.value = ''
}
</script>
<template>

  <!-- <div class="common-layout">
    <el-container>
       <el-aside width="200px">{{ id }}</el-aside> 
      <el-container>
        <el-header>与{{ nameStore.nameList[userStore.book] }}的私聊</el-header>
        <div style="height: 600px; background-color: #efefef; overflow: auto" ref="Main">
          <div v-for="item in privateNewsStore.privateNewsList" :key="item.id"
            :class="{ textGreen: item.fromUser == userStore.userName }" ref="rol">
            <div style="padding-top: 12px; padding-left: 1px">
              {{ nameStore.nameList[item.fromUser] }}({{ item.fromUser }}) :
            </div>
            <div style="padding-left: 10px">{{ item.content }}</div>
          </div>
        </div> -->

        <div class="common-layout">
        <el-container>
          <el-header>与{{ nameStore.nameList[userStore.book] }}的私聊</el-header>
          <div class="container">
        <el-scrollbar ref="scrollbarRef" class="scroll">
          <div v-for="item in privateNewsStore.privateNewsList" :key="item.id"
            :class="{ 'user-talk': item.fromUser == userStore.userName,'talk':true }" ref="rol">
            <div class="avatar">
              {{ nameStore.nameList[item.fromUser] }}
            </div>
            <div>
              <div class="talk-name">
                {{ nameStore.nameList[item.fromUser] }}({{ item.fromUser }})
              </div>
              <div class="talk-content">{{ item.content }}</div>
            </div>
          </div>
        </el-scrollbar>
         <!-- <div ref="roll" style="padding: 50px 0px">  -->
      </div>
        <el-footer class="bottom">
        <el-input @keydown.enter.ctrl="sendNews" v-model="news" placeholder="ctrl+回车快速发送">
        </el-input>
        <el-button @click="sendNews">发送</el-button>
      </el-footer>
    </el-container>
  </div>
</template>

<style lang="scss" scoped>
.class {
  height: 2000;
}

.textGreen {
  color: green;
}
.container{
  background-color: rgb(239, 239, 239);
  height: calc(80vh - 50px); ;

  padding: 10px;
}
.title{
  font-size: 18px;
  color: #409eff;
  margin-bottom: 20px;
  width: fit-content;
  &-idx{
    font-size: 14px;
    color: #909399;
    margin-left: 10px;
    cursor: pointer;
  }
}
.user-talk{
  margin-left: auto;
  flex-direction: row-reverse;
  .talk-name{
    text-align: right;
  }
}
.talk{
  display: flex;
  align-items: start;
  &-name{
    font-size: 13px;
    color: #959595;
    margin-top: 10px;
  }
  &-content{
    font-size: 14px;
    color: #303133;
    margin-top: 5px;
    background-color: #fff;
    border-radius: 15px;
    max-width: 50vw;
    padding: 5px;
    word-wrap: break-word;
    word-break: break-all;
  }
}
.avatar{
  width: 30px;
  flex-shrink: 0;
  height: 30px;
  font-size: 16px;
  background-color: #00318b;
  color: #cccccc;
  border-radius: 50%;
  text-align: center;
  line-height: 28px;
  margin-top: 10px;
  margin-left: 10px;
  overflow: hidden;
}
.bottom{
  display: flex;
  align-items: center;
  position: fixed;
  bottom: 10px;
  justify-content: center;
  width: calc(100% - 300px);
  padding: 0 40px;
  gap: 20px;
}
</style>
